package com.wey.springboot.controller;

import com.wey.springboot.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@ResponseBody
public class HelloController {
  @Autowired
  StringRedisTemplate stringRedisTemplate;

  @GetMapping("/hello")
  public CommonResult<String> hello() {
    return CommonResult.success("hi");
  }

  @GetMapping("/test")
  public CommonResult<String> test() {
    ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
    operations.set("key", "hello redis");
    return CommonResult.success(operations.get("key"));
  }

  @GetMapping("urlCount")
  public CommonResult<Map<String, Object>> urlCount() {
    HashOperations<String, String, Object> operations = stringRedisTemplate.opsForHash();
    return CommonResult.success(operations.entries("urlCount"));
  }
}
